<template>
  <view>
    <!-- 搜索区域 -->
    <view class="search-box">
      <uni-search-bar :focus="true" v-model="searchValue" @input="input" :radius="100" cancelButton="none">
      </uni-search-bar>
    </view>
    <!-- 显示区域 -->
    <view class="sugg-list" v-if="searchList.length !== 0">
      <!-- 商品列表 -->
      <uni-list>
        <uni-list-item v-for="item in searchList" :title="item.goods_name" link :key="item.goods_id"
          @click="goDetail(item)"></uni-list-item>
      </uni-list>
    </view>
    <!-- 搜索历史 -->
    <view class="history-box" v-else>
      <view class="history-title">
        <text>搜索历史</text>
        <uni-icons type="trash" size="17" @click="clearHistor"></uni-icons>
      </view>
      <!-- 列表区域 -->
      <view class="history-list">
        <uni-tag :text="item" v-for="(item, i) in historyList" :key="i" @click="clickHistory(item)"></uni-tag>
      </view>
    </view>
  </view>
</template>

<script>
  export default {
    data() {
      return {
        // 输入框文字
        searchValue: '',
        timer: null,
        // 搜索结果列表
        searchList: [],
        // 搜索历史列表
        historyList: []
      };
    },
    methods: {
      // 点击跳转到商品详情
      goDetail(row) {
        uni.navigateTo({
          url: '/subpkg/goods_detail/goods_detail?goods_id=' + row.goods_id
        })
      },
      // 清空搜索历史
      clearHistor() {
        this.historyList = []
        // 清空本地存储中记录的搜索历史
        uni.setStorageSync('kw', '[]')
      },
      // 点击历史纪录触发搜索
      clickHistory(row) {
        this.searchValue = row
      },
      // 输入事件
      input(e) {
        // 防抖
        clearTimeout(this.timer)
        this.timer = setTimeout(() => {
          this.searchValue = e
          this.getSearchList()
        }, 500)
      },
      // 通过关键词搜索信息
      async getSearchList() {
        // 初始化数据
        if (this.searchValue.length === 0) {
          this.searchList = []
          return
        }
        const {
          data: res
        } = await uni.$http.get('v1/goods/qsearch', {
          query: this.searchValue
        })
        if (res.meta.status !== 200) return uni.$showMsg
        this.searchList = res.message
        if (this.searchList.length === 0) {
          uni.$showMsg('暂无商品')
        }
        // 判断搜索词是否重复
        if (this.historyList.includes(this.searchValue)) return
        this.historyList = [this.searchValue, ...this.historyList]
        // 调用 uni.setStorageSync(key, value) 将搜索历史记录持久化存储到本地
        uni.setStorageSync('kw', JSON.stringify(this.historyList))
      }
    },
    onLoad() {
      this.historyList = JSON.parse(uni.getStorageSync('kw') || '[]')
    }
  }
</script>

<style lang="scss">
  page {
    // background-color: #fff;

    // 搜索区域
    .search-box {
      position: sticky;
      top: 0;
      z-index: 999;

      .uni-searchbar {
        background-color: #C00000;
      }
    }

    // 显示区域
    .uni-list-item__content-title {
      // 文字不允许换行（单行文本）
      white-space: nowrap;
      // 溢出部分隐藏
      overflow: hidden;
      // 文本溢出后，使用 ... 代替
      text-overflow: ellipsis;
      margin-right: 3px;
    }

    // 搜索历史
    .history-box {
      padding: 0 5px;

      .history-title {
        display: flex;
        justify-content: space-between;
        align-items: center;
        height: 40px;
        font-size: 13px;
        border-bottom: 1px solid #efefef;
        margin-bottom: 10rpx;
      }

      .history-list {
        display: flex;
        flex-wrap: wrap;

        .uni-tag {
          margin-top: 5px;
          margin-right: 5px;
        }
      }
    }

  }
</style>
